package com.ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;
import java.sql.*;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.*;

import com.dao.CartePreparedRequete;
import com.dao.Connecter;
import com.ihm.FenetreEdition ;

public class FenetreAjouter extends JDialog
{
    private CartePreparedRequete carteps;
    private Connecter connect;
    private JLabel Title;
    private JLabel titre;
    private static JTextField texttitre;
    private JLabel Description;
    private static JTextArea TextDescription;
    private JLabel image;
    private JButton buttonparcourir;
    private  static JTextField path;
    
    public static JTextField getPath() {
        return path;
    }
    
    public static void setPath(JTextField path) {
        FenetreAjouter.path = path;
    }
    
    private JButton buttonvalider;
    private JButton buttoneffacer;
    private JButton buttonannuler;
    private JPanel panelTitle;
    private JPanel paneltitre;
    private JPanel panelDescription;
    private JPanel panelimage;
    private JPanel panelbutton;
    private JPanel mainpanel;
    private static JFileChooser file;
    private Connecter connector;
    
    public FenetreAjouter(String text)
    {
        this.setTitle("Mini S.I.G : " + text);
        this.setSize(600,400);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        //this.setLayout(new BorderLayout());
        //this.setBackground(Color.WHITE);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setModal(true);
        

          createcomponent();
            this.getContentPane().add(mainpanel);
	     event();	
		this.setVisible(true);
	}
        
	public static JTextField getTexttitre() {
		return texttitre;
	}
        
	public static void setTexttitre(JTextField texttitre) {
		FenetreAjouter.texttitre = texttitre;
	}
        
	public static JTextArea getTextDescription() {
		return TextDescription;
	}
        
	public static void setTextDescription(JTextArea textDescription) {
		TextDescription = textDescription;
	}
        
	public void close()
	{
		this.dispose();
	}
    public void event()
    {
        buttonannuler.addMouseListener(new MouseListener()
	{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stu
			close();
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	});
          
	buttoneffacer.addMouseListener(new MouseListener()
	{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			if(texttitre!=null&&TextDescription!=null)
			{
				texttitre.setText(null);
				TextDescription.setText(null);
				path.setText(null);
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	});
          
	buttonparcourir.addMouseListener(new MouseListener()
	{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			file = new JFileChooser(".");
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Choisir un fichier image","png","jpg","gif");
			file.setFileFilter(filter);
			int result = file.showOpenDialog(null);
			path.setText(null);
			if(result==JFileChooser.APPROVE_OPTION)
			{   
				
				{String path1=file.getSelectedFile().getAbsolutePath();
				path.setText(path1);
				}

			}

		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	});
          
	buttonvalider.addMouseListener(new MouseListener()
	{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			// TODO Auto-generated method stub
			carteps = new CartePreparedRequete();
			if(!texttitre.getText().toString().isEmpty()&&!path.getText().toString().isEmpty())
			{	
			try{
				carteps.Ajouter();
			}
			catch(SQLException e)
			{
				e.printStackTrace();
				throw new RuntimeException("L'erreur dans le script SQL ,le programme va s'arreter");
			}
			FenetreConsultation.getPanelmiddle().removeAll();
            FenetreEdition.getPanelmiddle().removeAll();
			try {
				carteps.afficher();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			close();
			FenetreConsultation.getPanelmiddle().setMaximumSize(new Dimension(700,500));
			FenetreConsultation.getPanelmiddle().setSize(700,500);
            FenetreConsultation.createlist();
            FenetreEdition.getPanelmiddle().setMaximumSize(new Dimension(700,500));
            FenetreEdition.getPanelmiddle().setSize(700,500);
			FenetreEdition.createlist();
			
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Veuillez-vous remplir le champs titre et ajouter un image");
			}
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}
		
	});
	
    }
	public void createcomponent()
	{
		Title= new JLabel("Ajouter une carte",Title.CENTER);
		Title.setFont(new Font("italique",Font.BOLD,20));
		panelTitle = new JPanel();
		panelTitle.add(Title);
		//***********************************//
		titre= new JLabel("Titre");
                titre.setFont(new Font("italique",Font.BOLD,15));
		texttitre = new JTextField();
		texttitre.setMaximumSize(new Dimension(150,20));
		texttitre.setSize(new Dimension(150,20));
		paneltitre = new JPanel();
		paneltitre.setLayout(new BoxLayout(paneltitre, BoxLayout.X_AXIS));
                paneltitre.add(titre);
                paneltitre.add(Box.createHorizontalStrut(20));
                paneltitre.add(texttitre);
                //***********************************//
                Description = new JLabel("Description");
                Description.setFont(new Font("italique",Font.BOLD,15));
                TextDescription = new JTextArea();
                TextDescription.setMaximumSize(new Dimension(200,100));
                TextDescription.setSize(new Dimension(250,150));
                panelDescription = new JPanel();
                panelDescription.setLayout(new BoxLayout(panelDescription,BoxLayout.X_AXIS));
                panelDescription.add(Description);
                panelDescription.add(Box.createHorizontalStrut(20));
                panelDescription.add(TextDescription);
                //***********************************//
                image = new JLabel("Image");
                image.setFont(new Font("italique",Font.BOLD,15));
                buttonparcourir = new JButton("parcourir");
                buttonparcourir.setSize(new Dimension(60,20));
                path = new JTextField();
                path.setMaximumSize(new Dimension(150,20));
                path.setSize(new Dimension(150,20));
                path.setEditable(false);
                panelimage = new JPanel();
                panelimage.setLayout(new BoxLayout(panelimage,BoxLayout.X_AXIS));
                panelimage.add(image);
                panelimage.add(Box.createHorizontalStrut(20));
                panelimage.add(buttonparcourir);
                panelimage.add(Box.createHorizontalStrut(20));
                panelimage.add(path);
  
                //**************************************//
                buttonvalider= new JButton("Enregistrer");
                buttonvalider.setSize(new Dimension(60,20));
                buttoneffacer=new JButton("Effacer");
                buttoneffacer.setSize(new Dimension(60,20));
                buttonannuler= new JButton("Annuler");
                buttonannuler.setSize(new Dimension(60,20));
                panelbutton = new JPanel();
                panelbutton.setLayout(new BoxLayout(panelbutton,BoxLayout.X_AXIS));
                panelbutton.add(Box.createHorizontalBox());
                panelbutton.add(Box.createHorizontalGlue());
                panelbutton.add(buttonvalider);
                panelbutton.add(Box.createHorizontalStrut(20));
                panelbutton.add(buttoneffacer);
                panelbutton.add(Box.createHorizontalStrut(20));
                panelbutton.add(buttonannuler);
                panelbutton.add(Box.createHorizontalStrut(20));
                //*****************************************//
                mainpanel = new JPanel();
                mainpanel.setLayout(new BoxLayout(mainpanel,BoxLayout.Y_AXIS));
                mainpanel.add(panelTitle);
                mainpanel.add(paneltitre);
                mainpanel.add(Box.createVerticalStrut(40));
                mainpanel.add(panelDescription);
                mainpanel.add(Box.createVerticalStrut(80));
                mainpanel.add(panelimage);
                mainpanel.add(Box.createVerticalStrut(40));
                mainpanel.add(panelbutton);
	}
		
};